package com.gs.djys.webview;


import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import com.gs.djys.Base.Urls;
import com.gs.djys.R;
import com.gs.djys.Util.ToastUtils;
import com.gs.djys.mvp.MVPBaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class WebViewActivity extends MVPBaseActivity<WebViewContract.View, WebViewPresenter> implements WebViewContract.View {

    @Bind(R.id.webview)
    MyWebView mWebview;
    @Bind(R.id.relativeLayout)
    RelativeLayout relativeLayout;
    @Bind(R.id.backs)
    RelativeLayout backs;
    private String reqNo, sign;

    @Override
    public void onRequestError(String msg) {
        ToastUtils.showShortToast(msg);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_web;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getExtras();
        reqNo = bundle.getString("req_no");
        sign = bundle.getString("sign");
        //启用数据库
        mWebview.getSettings().setDatabaseEnabled(true);

        //设置定位的数据库路径
        String dir = this.getApplicationContext().getDir("Code_Activity", Context.MODE_PRIVATE).getPath();
        mWebview.getSettings().setGeolocationDatabasePath(dir);

        //启用地理定位
        mWebview.getSettings().setGeolocationEnabled(true);
        mWebview.getSettings().setJavaScriptEnabled(true);
        //开启DomStorage缓存
        mWebview.getSettings().setDomStorageEnabled(true);
        //帮助WebView处理各种通知、请求事件
        mWebview.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//                view.getSettings().setBlockNetworkImage(true);
//                view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
                return false;
            }


            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

                //开始
                /**
                 * 网页重定向时会执行多次
                 */
            }

            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
                //网页加载成功
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //网址处理
                /**
                 * 可对指定网址进行拦截
                 */
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError
                    error) {
                super.onReceivedError(view, request, error);
                //网页加载失败
                /**
                 * 此回调中可进行自定义错误页面，
                 * 遇到错误时示例代码:view.loadUrl("file://android_asset/error.html");
                 */
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //网页加载完成
//            view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//            view.getSettings().setBlockNetworkImage(false);
            }
        });

        mWebview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedIcon(WebView view, Bitmap icon) {
                super.onReceivedIcon(view, icon);

            }

            //辅助WebView处理Javascript的对话框，网站图标，网站title，加载进度等
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                //网页加载进度
                if (newProgress < 100) {

                }
            }

            @Override
            public void onGeolocationPermissionsShowPrompt(String
                                                                   origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
                super.onGeolocationPermissionsShowPrompt(origin, callback);

            }
        });
        mWebview.loadUrl(Urls.SERVER + "/road/zclic/app/kyd_view.html?req_no=" + reqNo + "sign=" + sign);
        Log.e("H5地址测试", Urls.SERVER + "/road/zclic/app/kyd_view.html?req_no=" + reqNo + "sign=" + sign);
        backs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
